package com.xin.ruanjian.omsystem.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputEditText;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.xin.ruanjian.omsystem.R;
import com.xin.ruanjian.omsystem.app.MyApplication;
import com.xin.ruanjian.omsystem.bases.BaseActivity;
import com.xin.ruanjian.omsystem.bases.Login;
import com.xin.ruanjian.omsystem.utils.HttpUtils;
import com.xin.ruanjian.omsystem.utils.PhoneFormatCheckUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.xin.ruanjian.omsystem.bases.Contants.USER_LOGIN;


public class LoginActivity extends BaseActivity {
    @BindView(R.id.tv_centerTitle)
    TextView tvCenterTitle;
    @BindView(R.id.iv_rightPic)
    ImageView ivRightPic;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_phone)
    TextInputEditText tvPhone;
    @BindView(R.id.tv_pwd)
    TextInputEditText tvPwd;
    @BindView(R.id.btn_login)
    Button btnLogin;

    private boolean isNeedToMain = false;

    @Override
    protected void initContentView(Bundle bundle) {
        setContentView(R.layout.activity_login);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initData() {
        isNeedToMain = getIntent().getBooleanExtra("jump", true);
    }

    @Override
    protected void main() {

    }

    private void Login() {
        if (TextUtils.isEmpty(tvPhone.getText())) {
            Toast("手机号输入不能为空！");
            return;
        } else if (!PhoneFormatCheckUtils.isPhoneLegal(tvPhone.getText().toString())) {
            Toast("请输入正确的手机号");
        }
//        else if (TextUtils.isEmpty(tvPwd.getText())) {
//            Toast("密码不能为空！");
//            return;
//        }
        showLoadingDialog();
        HttpUtils httpUtils = new HttpUtils(USER_LOGIN) {
            @Override
            public void onError(Call call, Exception e, int id) {
                showErrorDialog();
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                try {
                    JSONObject o = new JSONObject(response);
                    int state = o.getInt("state");
                    if (state == 1) {
                        Toast("登录成功");
                        Login login = MyApplication.getLogin();
                        if (login == null) { login = new Login();}
                        login.setUserId(o.getJSONObject("msg").getInt("userid"));
                        login.setToken(o.getJSONObject("msg").getString("token"));
                        MyApplication.saveLogin(login);
                        if (isNeedToMain) { startActivity(new Intent(context, MainActivity.class)); }
                        finish();
                    }else {
                        showErrorDialog("登录失败");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };
        httpUtils.addParam("userphone", tvPhone.getText().toString());
        httpUtils.addParam("pwd", tvPwd.getText().toString());
        httpUtils.clicent();
    }

    @OnClick(R.id.btn_login)
    public void onViewClicked() {
        Login();
    }
}

